var $=KISSY.all;
// KISSY.use("node",function(S,Node){
// var $=Node.all; 
var top_sign = $(".sign").val();
var sid = $(".sid").val();
var usernick;
function __dialog(title,addon,top,width){
			var tstr = top?"top:"+top+"px;":"";
			var wstr = width?"width:"+width+"px;":"width:830px";
			var dialog =("<div class=\"bui-dialog\" style=\""+wstr+"visibility:visible;display:block;"+tstr+"\">");
			dialog+=("    <div class=\"bui-stdmod-header\">");
			dialog+=("        <div class=\"header-title\">");
			dialog+=("            "+title+"");
			dialog+=("        </div>");
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-body\">");
			dialog+=("    "+addon);
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-footer\">");
			dialog+=("        <button class=\"button button-primary cancel\">");
			dialog+=("            关闭");
			dialog+=("        </button>");
			dialog+=("    </div>");
			dialog+=("    <a class=\"bui-ext-close\">");
			dialog+=("        <span class=\"bui-ext-close-x x-icon x-icon-normal\">");
			dialog+=("            ×");
			dialog+=("        </span>");
			dialog+=("    </a>");
			dialog+=("</div>");
			return dialog;
}
function showDialog(title,addon,show,top){
	var top=top?top:($("."+show).outerHeight())/2;
	var width=($("."+show).innerWidth());
	// var mask = ("<div style=\"width:100%;left:0;top:0;height:100%;position:fixed;\" class=\"bui-ext-mask\"></div>");
	// $(mask).insertBefore("."+show);
	$(__dialog(title,addon,top,width)).insertBefore("."+show);
	$(".bui-ext-close-x").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
	$(".cancel").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
	// $(".bui-dialog").scrollIntoView();
}
function closeDialog(){
$(".bui-ext-close-x").fire("click");
}
$(".js-suoqu").on("click",function(){
	var fuid = $(this).attr("data-fuid");
	suoqu(fuid);
});
$(".js-zengsong").on("click",function(){
	var fuid = $(this).attr("data-fuid");
	suoqu(fuid,"zengsong");
});
$(".qiandao").on("click",function(){
		KISSY.io({
		url: "/?a=qiandao&m=front&c=index",
		success: function(data, textStatus) {
			if(data.login==false){
				showDialog("尚未登录","您尚未登录，请登录后继续！<br />","buyers");
				$(".bui-stdmod-body").css("text-align","center");
				$(".J_TB_Login").clone(true,true,true).appendTo(".bui-stdmod-body").removeClass("hide");
			}else{
				alert(data.msg);
			}
		},
		complete: function(data){
			
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
});
// $(".buyer").on("mouseout",function(){
	// $(".refresh").hide();
// });
$(".buyer").on("mouseenter",function(){
	$(".refresh").show();
});
$(".gg,.gz,.dj").on("click",function(){
	var tp = $(this).attr("data-tp");
	var sid = $(".sid").val();
	switch(tp){
		case "gg" : var t = "活动公告";break;
		case "gz" : var t = "游戏规则";break;
		case "dj" : var t = "兑奖方式";break;
	}
	KISSY.io({
		url: "/?a=getgg&m=shopmodule",
		data:{"tp":tp,"sid":sid},
		success: function(data, textStatus) {
			showDialog(t,"<span style=\"font-size:12px\">"+data.msg+"</span>","event",100);
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
});
var usrpg=0;
$(".js-showicon").on("mouseenter mouseout",function(e){
	var togid = $(this).attr("data-id");
	if(e.type=="mouseenter"){
	$("."+togid).removeClass("hide");
	}else{
	$("."+togid).addClass("hide");
	}
	
});
$(".refresh").on("click",function(){
	KISSY.io({
		url: "/?a=rfrusers&m=front&c=event",
		data:{"pg":usrpg},
		success: function(data, textStatus) {
			if(data.status==false){
				alert("已经没有更多用户了！");
				return;
			}
			var usr="";
			for(var i=0;i<data.length;i++){
				usr+=("<div class=\"buyer\">");
				usr+=("        <div class=\"avatar\">");
				usr+=("                    <p><img src=\""+data[i].avatar+"\"></p>");
				usr+=("                    <a class=\"suo js-suoqu\" data-fuid=\""+data[i].id+"\"><img src=\"http://img04.taobaocdn.com/imgextra/i4/27390555/TB2ouFfaXXXXXbYXpXXXXXXXXXX_!!27390555.jpg\"></a>");
				usr+=("                </div>");
				usr+=("                <div class=\"stat\">");
				usr+=("                    <p>抽奖次数：<span>"+data[i].lotts+"次</span></p>");
				usr+=("                    <p>中奖次数：<span>"+data[i].wins+"次</span></p>");
				usr+=("                    <p>中奖率：<span>"+data[i].val+"/100</span></p>");
				usr+=("                    ");
				usr+=("                    <a href=\"/?m=front&c=event#\"><img src=\"http://img04.taobaocdn.com/imgextra/i4/27390555/TB2vuhfaXXXXXX4XpXXXXXXXXXX_!!27390555.jpg\" style=\"border:0px;\"></a>");
				usr+=("                </div>");
				usr+=("    </div>");
			}
			$(".buyer").remove();
			$(usr).appendTo(".buyers");
			$(".js-suoqu").on("click",function(){
				var fuid = $(this).attr("data-fuid");
				suoqu(fuid);
			});
			usrpg++;
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
});
function suoqu(fuid,action){
if(!action){
	action="suoqu";
}
KISSY.io({
	url: "/?a="+action+"&m=front&c=index",
	data: {"fuid":fuid},
	success: function(data, textStatus) {
		closeDialog();
		if(data.login==false){
			showDialog("尚未登录","您尚未登录，请登录后继续！<br />","buyers");
			$(".bui-stdmod-body").css("text-align","center");
			$(".J_TB_Login").clone(true,true,true).appendTo(".bui-stdmod-body").removeClass("hide");
		}else{
			alert(data.msg);
		}
	},
	complete: function(data){
		
	},
	error: function(){
		alert('error');
	},
	dataType:"jsonp"
});
}
var clicktp;
TOP.ui("authbtn", {
     type:'mini',
     onload:function(handler){ 
		$(".dolott,.action,.gua").on("click",function(){
			if(!usernick){
				handler();
			}else{
				dolott();
			}
		});
		$(".mygift").on("click",function(){
			clicktp=true;
			if(!usernick){
				handler();
			}else{
				showmygift(usernick);
			}
		});
     },
     callback: function(data){ 
		if(data.success==true){
			usernick = (data.result.taobao_user_nick); 
			if(clicktp){
				showmygift(usernick);
			}else{
			dolott();
			}
		}else{
			$(".J_TB_Login").fire("click");
			alert("not login");
			// $(".allow_over").show();
			// $(".btn-allow,.btn-noallow,.ks-ext-close-x").on("click",function(){
				// $(".allow_over").hide();
			// });
			// $(".btn-allow").on("click",function(){
				// $(".dig-button").fire("click");
			// });
			// $(".top-login-btn-container").show();
		}
	 }
});
function gameDialog(title,msg,show,oid,width){
// var top=($("."+show).outerHeight())/2;
width=width?width:($("."+show).innerWidth())/2; 
var margin=(990-width)/2;
var top=100;
var tstr = top?"top:"+top+"px;":"";
var wstr = width?"width:"+width+"px;":"width:830px";
// var mask = ("<div style=\"width:100%;left:0;top:0;height:100%;position:fixed;\" class=\"bui-ext-mask\"></div>");
// $(mask).insertBefore("."+show);
var temp = '';
temp+='<div class="js-gmdlg ks-dialog ks-overlay allow_over" style="'+wstr+'margin-left:'+margin+'px;margin-right:'+margin+'px;left:0;visibility:visible;z-index:9;display:block;'+tstr+'">';
temp+='	<div class="ks-contentbox" style="height:auto">';
temp+='		<div class="ks-stdmod-header" style="text-align:left">'+title+'</div>';
temp+='		<div class="ks-stdmod-body">';
temp+='			<div class="con-als">';
temp+='			'+msg;
temp+='			</div>';
if(oid){
temp+='			<div style="text-align:center"><input class="btn-mini btn-info js-dolott" type="button" value="继续抽"><input class="btn-mini btn-info js-ok" type="button" value="知道了"></div>';
}else{
temp+='			<div style="text-align:center"><input class="btn-mini btn-info js-ok" type="button" value="知道了"></div>';
}
temp+='		</div>';
temp+='		<div class="ks-stdmod-footer"></div>';
temp+='	</div>';
temp+='	<a class="ks-ext-close" role="button" href="javascript:void(" 关闭")"="" tabindex="0">';
temp+='		<span class="ks-ext-close-x js-close">关闭</span>';
temp+='	</a>';
temp+='</div>';
$(temp).appendTo("."+show);
$(".js-close,.js-ok").on("click",function(){
	$(".js-gmdlg").remove();
});
$(".js-dolott").on("click",function(){
	var oid = $(".oid").val();
	dolott(oid);
});
}
function dolott(oid){
	$(".js-gmdlg").remove();
	oid = oid?oid:"";
	var ti = new Date().getTime();
	var tool_type = $(".tool_type").val();
	var ddcj = $(".ddcj").val();
	if(tool_type==1){
		var sign = R(sid+"|"+oid+"|"+usernick+"|"+top_sign+"|"+"ntPfLBg"+ti);
		var data = {"shop_id":sid,"usernick":usernick,"sign":sign,"top_sign":top_sign,"oid":oid,"ts":ti};
		var url = "/?a=normallot&m=Shopmodule";
	}
	if(tool_type==3){
		var sign = R(sid+"|"+oid+"|"+usernick+"|"+lastscoreid+"|"+lastrange+"|"+lotts+"ntPfLBg"+ti);
		var data = {"shop_id":sid,"usernick":usernick,"lastscoreid":lastscoreid,"lotts":lotts,"lastrange":lastrange,"sign":sign,"top_sign":top_sign,"oid":oid,"ts":ti};
		var url = "/?a=dolotts&m=Shopmodule";
	}
	// var sign = R("123");
	KISSY.io({
		"url": url,	
		"data":data,
		"success": function(data, textStatus) {
			if(data.status==true){
				var t = "中奖啦！";
			}else{
				var t = "抱歉，您没中奖";
			}
			var msg="";
			var style="margin-top:25px;text-align:center";
			if(data.score==0&&ddcj){
				msg = "您还可以输入订单号抽奖！<br />订单号：<input type=\"text\" class=\"oid\" />";
				style="margin-top:25px;margin-left:40px;text-align:left";
				var oid = true;
			}
			doanim(data,t,"<div style=\""+style+"\">"+data.giftname+msg+"</div>","event",oid);
			return;
			gameDialog(t,"<div style=\""+style+"\">"+data.giftname+msg+"</div>","event",oid);
		},
		"error": function(){
			alert('error');
		},
		//发送请求类型是jsonp
		"dataType":"jsonp"
	});
}
$(".sns-like").on("click",function(){
mission("likeme",$(this).attr("data-eid"));
});
$(".sns-followshop").on("click",function(){
mission("collect",$(this).attr("data-eid"));
});
$(".sns-friendfollow").on("click",function(){
mission("followme",$(this).attr("data-eid"));
});
$(".sns-sharebtn").on("click",function(){
mission("shareme",$(this).attr("data-eid"));
});

function mission(mis,eid){
	KISSY.io({
		url: "/?a=mission&m=front&c=event",
		data: {"mis":mis,"eid":eid},
		success: function(data, textStatus) {
		closeDialog();
			if(data.status==false){
				showDialog("出错","完成任务出错。","event");
			}else{
				//showDialog("成功！","恭喜您完成任务！","event");
			}
		},
		complete: function(data){
			
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
}

var wheels;
function getgift(data){
	if(data.status==true && data.level=="5"){
		var gft = "中奖";
		return gft;
	}
		switch(data.level){
			case "1" : var gft = "一等奖"; break;
			case "2" : var gft = "二等奖"; break;
			case "3" : var gft = "三等奖"; break;
			case "4" : var gft = "鼓励奖"; break;
			default : 
				var gt = ["大奖在即！","再接再厉","大奖不远啦！","亲，加油！","谢谢参与"];
				var gft =gt[parseInt(4*Math.random())];
			break;
		}
		return gft;
}
function doanim(data,t,msg,selec,oid){
	var curtool = $(".curtool").val();
	//水果机的动画
	if(curtool=="fruit"){
		gameDialog(t,msg,selec,oid);
	}
	//随机六边形的动画
	if(curtool=="chest"){
		if(data.score>0){
			$(".chest_ac").hide();
			$(".js-t").remove();
			var gft = getgift(data);
			if(itv){clearInterval(itv);}
			var last_chest;
			var ts=0;
			var itv = setInterval(function (){
				if(last_chest){$(".chest"+last_chest).hide();}
				var i = parseInt(9*Math.random()+1);
				$(".chest"+i).show();
				last_chest=i;
				if(ts>=2000){
				clearInterval(itv);
					if(data.status==true){
						var t = "奖品藏在这";
					}else{
						var t = "天气真好";
					}
					$(".chest_ac ").html("<div class=\"js-t\" style=\"font-size:12px;color:#FF3A3A;margin-top:48px;text-align: center;\">"+gft+"</div>");
					$(".chest_ac ").on("click",function(){
					gameDialog(t,msg,selec,oid);
					});
				}
				ts+=30;
			},30);
		}else{
			gameDialog(t,msg,selec,oid);
		}
	}
	//刮刮卡的动画
	if(curtool=="scratchcards"){
		if(data.score>0){
		var gft = getgift(data);
		$(".g").remove();
		var g = '<div class="g" style="position:absolute;top: 205px;width:174px;text-align:center;z-index:0">'+gft+'</div>';
		$(g).insertAfter(".gua");
			// KISSY.use("anim", function(S, Anim){
				var b = new KISSY.Anim('.gua',{"width": 0}, 1 ,'',function(){gameDialog(t,msg,selec,oid);});
				b.run();
			// });
		}else{
			gameDialog(t,msg,selec,oid);
		}
	}
	//大富翁的动画
	if(curtool=="dices"){
		for(var i=1;i<15;i++){
			$(".dd"+i).css("-moz-opacity",0);
			$(".dd"+i).css("opacity",0);
			$(".dd"+i).css("filter","alpha(opacity=0)");
		}
		if(itv){clearInterval(itv);}
		if(data.score>0){
			var row = parseInt(3*Math.random()+1);
			var gift=0;
			switch(data.level){
				case "1" :gift = 4;	break;
				case "2" :gift = 6;	break;
				case "3" :gift = 12;break;
				case "4" :gift = 10;break;
				case "5" :gift = 10;break;
				default : 
					var s = [2,3,5,9,11,13,15];
					gift = s[parseInt(6*Math.random())];
				break;
			}
			var alp = 100;
			var dd = 1;
			var ts = 0;
			var ad = 14;
			var rowcont = 0;
			var itv = setInterval(function (){
				var ddadd =[0,1,2,3,4];
				if(dd<6){
					for(var i=0;i<ddadd.length;i++){
						$(".dd"+(ad-ddadd[i])).css("-moz-opacity",1-(0.2*(i+1)));
						$(".dd"+(ad-ddadd[i])).css("opacity",1-(0.2*(i+1)));
						$(".dd"+(ad-ddadd[i])).css("filter","alpha(opacity="+100-(20*(i+1))+")");
					}
					ad ++;
				}
				for(var i=0;i<ddadd.length;i++){
					$(".dd"+(dd-ddadd[i])).css("-moz-opacity",1-(0.2*(i+1)));
					$(".dd"+(dd-ddadd[i])).css("opacity",1-(0.2*(i+1)));
					$(".dd"+(dd-ddadd[i])).css("filter","alpha(opacity="+100-(20*(i+1))+")");
				}
				dd++;
				if(dd==15){
					dd=1;
					ad=14;
					rowcont++;
				}
				if(dd==gift&&row==rowcont){
					for(var i=1;i<15;i++){
						if(i!=gift-1){
						$(".dd"+i).css("-moz-opacity",0);
						$(".dd"+i).css("opacity",0);
						$(".dd"+i).css("filter","alpha(opacity=0)");
						}
					}
					clearInterval(itv);
					gameDialog(t,msg,selec,oid);
				}
				ts += 30;
			},60);
		}else{
			gameDialog(t,msg,selec,oid);
		}
	}
	//砸金蛋的动画
	if(curtool=="eggs"){
		gameDialog(t,msg,selec,oid);
	}
	//大转盘的动画
	if(curtool=="wheels"){
		var on = new Array();
		if(data.score==0){
			gameDialog(t,msg,selec,oid);
			return;
		}
		if(data.status==true){
			switch(data.level){
				case "1" :
				on.push(32);
				on.push(25.5);
				break;
				case "2" :
				on.push(24);
				on.push(30.5);
				break;
				case "3" :
				on.push(22.5);
				on.push(28.5);
				break;
				default:
				on.push(22);
				on.push(28);
				break;
			}
			wheel(on[parseInt(1*Math.random())],2,t,msg,selec,oid);
		}else{
			on.push(21);
			on.push(29.5);
			on.push(31);
			on.push(24.5);
			on.push(27);
			
			wheel(on[parseInt(4*Math.random())],2,t,msg,selec,oid);
		}
	}
}
function wheel(thdown,fast,t,msg,selec,oid){
	var a,b,c,d,e,f,th=0,fast=fast;
	if(wheels){clearInterval(wheels);}
	wheels=setInterval(function (){
		a = Math.cos(th/fast);
		b = Math.sin(th/fast);
		c = -(Math.sin(th/fast));
		d = Math.cos(th/fast);
		e = 0;
		f = 0;
		$(".wheels_ac").css("-moz-transform","matrix("+a+", "+b+", "+c+", "+d+", "+e+", "+f+")");
		$(".wheels_ac").css("-webkit-transform","matrix("+a+", "+b+", "+c+", "+d+", "+e+", "+f+")");
		$(".wheels_ac").css("-o-transform","matrix("+a+", "+b+", "+c+", "+d+", "+e+", "+f+")");
		$(".wheels_ac").css("-ms-transform","matrix("+a+", "+b+", "+c+", "+d+", "+e+", "+f+")");
		$(".wheels_ac").css("transform","matrix("+a+", "+b+", "+c+", "+d+", "+e+", "+f+")");
		th++;
		// $(".gifts").html(th/fast);
		if(th/fast>=thdown){
			gameDialog(t,msg,selec,oid);
			clearInterval(wheels);
		}
	},30);
}
wheel(10000,200);
function showmygift(usernick,pg){
pg=pg?pg:0;
KISSY.io({
		url: "/?m=shopmodule&c=user",
		data: {"usernick":usernick,"page":pg},
		success: function(data) {
		$(".js-gmdlg").remove();
			var msg="";
				msg+=("<table class=\"table\">");
				msg+=("        <thead>");
				msg+=("          <tr>");
				msg+=("            <th></th>");
				msg+=("            <th>名称</th>");
				msg+=("            <th>价值</th>");
				msg+=("            <th>领取地址(复制后在浏览器内打开)</th>");
				msg+=("          </tr>");
				msg+=("        </thead>");
				msg+=("        <tbody>");
			for(var i in data.data){
				msg+=("          <tr>");
				msg+=("            <td></td>");
				msg+=("            <td>"+data.data[i].name+"</td>");
				msg+=("            <td>"+data.data[i].price+"</td>");
				msg+=("            <td>"+data.data[i].num_iids+"</td>");
				msg+=("          </tr>");
			}
				msg+=("        </tbody>");
				msg+=("    </table>");
				msg+=("<div class=\"pagination\" style=\"text-align:center\">");
				msg+=("        <ul>");
			var style=(pg==0||data.count==0)?"disabled":"";
				msg+=("          <li class=\""+style+" js-foward\" ><a href=\"#\">« 上一页</a></li>");
				msg+=("          ");
				msg+=("          ");
				msg+=("          ");
				msg+=("          ");
			var style=(pg*7>=data.count)?"disabled":"";
				msg+=("          <li class=\"js-next "+style+"\"><a href=\"#\">下一页 »</a></li>");
				msg+=("          <li><a href=\"#\" class=\"js-ok button\" style=\"color:#FF2D68\">关闭</a></li>");
				msg+=("        </ul>");
				msg+=("      </div>");
			gameDialog("您的奖品",msg,"event",false,900);
			$(".btn-mini").remove();
			if(pg*7<data.count){
				$(".js-next").on("click",function(){
					showmygift(usernick,++pg);
				});
			}
			if(pg>0){
			$(".js-foward").on("click",function(){
				showmygift(usernick,--pg);
			});
			}
		},
		error: function(){alert('error');},
		"dataType":"jsonp"
		});
}

// KISSY.use("anim", function(S, Anim)
// {
var b = new KISSY.Anim('.winlist',{scrollTop: 0}, 5 ,'',function(){t.run();});
var t = new KISSY.Anim('.winlist',{scrollTop: 100}, 5 ,'',function(){b.run();});
t.run();

//刮刮卡换一张
// var cisrun=false;
// $(".scratchcards_ac").on("click",function(){
	// $(".gua1").css("width","130px");
	// $(".gua").css("width","130px");
	// if(!cisrun){
		// $(".js-card1").css("left","398px");
		// $(".js-card1").css("display","block");
		// $(".js-card2").css("display","block");
		// var e = new KISSY.Anim('.js-card2',{"left": 398}, 1 ,'easeBothStrong',function(){
					// $(".js-card1").css("display","block");
					// $(".js-card2").css("display","none");
					// $(".gua").css("width","130px");
					// cisrun = false;
					// });
		// var d = new KISSY.Anim('.js-card1',{"left": 258}, 1 ,'easeBothStrong',function(){$(".js-card1").css("display","none");});
		// cisrun = true;
		// d.run();
		// e.run();
	// }
// });
var isrun;
$(".scratchcards_ac").on("click",function(){
	$(".gua1").css("width","130px");
	if(!isrun){
		$(".js-card1").css("left","398px");
		$(".js-card1").css("-moz-opacity","1");
		$(".js-card1").css("opacity","1");
		$(".js-card1").css("filter","alpha(opacity=100)");
		$(".js-card1").css("display","display");
		$(".js-card2").css("display","block");
		var e = new KISSY.Anim('.js-card2',{"left": 398,"-moz-opacity":1,"opacity":1,"filter":"alpha(opacity=100)"}, 1 ,'easeBothStrong',function(){
					$(".js-card1").css("left","398px");
					$(".js-card1").css("-moz-opacity","1");
					$(".js-card1").css("opacity","1");
					$(".js-card1").css("filter","alpha(opacity=100)");
					$(".js-card1").css("display","block");
					$(".js-card2").css("left","530px");
					$(".js-card2").css("-moz-opacity","0");
					$(".js-card2").css("opacity","0");
					$(".js-card2").css("filter","alpha(opacity=0)");
					$(".js-card2").css("display","none");
					$(".gua").css("width","130px");
					isrun = false;
					});
		var d = new KISSY.Anim('.js-card1',{"left": 258,"-moz-opacity":0,"opacity":0,"filter":"alpha(opacity=0)"}, 1 ,'easeBothStrong',function(){$(".js-card1").css("display","none");});
		isrun = true;
		d.run();
		e.run();
	}
});
// 水果机自动
var s = new KISSY.Anim('.js-fruit1',{"top": 0}, 3 ,'',function(){
		$(".js-fruit1").css("top","-82px");
		s.run();
	});
	s.run();
var s1 = new KISSY.Anim('.js-fruit2',{"top": 0}, 3 ,'',function(){
		$(".js-fruit2").css("top","-82px");
		s1.run();
	});
	s1.run();
var s2 = new KISSY.Anim('.js-fruit3',{"top": 0}, 3 ,'',function(){
		$(".js-fruit3").css("top","-82px");
		s2.run();
	});
	s2.run();
// });
// });
